﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CMSApplication.Models;
using CMSApplication.DataDictionaries;
using System.IO;

namespace CMSApplication.Logic
{
    public class ResourceManager
    {
        private static int size = 0;

        public List<ResourceModel> GetAllResources()
        {
            List<ResourceModel> resources = new List<ResourceModel>();

            if (Cache.ResourceCache.Count < size || size != 0)
            {
                using (var db = new CMSContext())
                {
                    resources = db.Resources.ToList();
                }

                foreach (var res in resources)
                {
                    if (!Cache.ResourceCache.Values.Contains(res))
                    {
                        Cache.ResourceCache.TryAdd(res.FileName, res);
                    }
                }
                size = resources.Count;
            }

            else
            {
                resources = Cache.ResourceCache.Values.ToList();
            }

            return resources;
        }

        public List<ResourceModel> GetResourcesByContentType(string contentType)
        {
            var resources = new List<ResourceModel>();

            if (Cache.ResourceCache.Count < size)
            {
                using (var db = new CMSContext())
                {
                    resources = db.Resources.Where(p => p.MimeType.Contains(contentType)).ToList();
                }
                foreach (var res in resources)
                {
                    if (!Cache.ResourceCache.ContainsKey(res.FileName))
                    {
                        Cache.ResourceCache.TryAdd(res.FileName, res);
                    }
                }
            }

            else
            {
                resources = Cache.ResourceCache.Values.Where(p => p.MimeType.Contains(contentType)).ToList();
            }

            return resources;
        }

        public ResourceModel GetResource(int id)
        {
            var resource = new ResourceModel();

            //Check the cache for resource first
            if (Cache.ResourceCache.Any(p => p.Value.ID == id))
            {
                resource = Cache.ResourceCache.Values.Single(p => p.ID == id);
            }
            else
            {
                using (var db = new CMSContext())
                {
                    resource = db.Resources.Where(p => p.ID == id) as ResourceModel;
                };
            }
            return resource;
        }

        public bool Save(HttpPostedFile file, int siteId)
        {
            var model = new ResourceModel();
           
            model.FileName = Path.GetFileName(file.FileName);
            model.MimeType = file.ContentType;
            model.SiteId = siteId;
            using (MemoryStream ms = new MemoryStream())
            {
                file.InputStream.CopyTo(ms);
                model.Stream = ms.GetBuffer();
            }
            Cache.ResourceCache.TryAdd(model.FileName, model);

            try
            {
                using (var db = new CMSContext())
                {
                    db.Resources.Add(model);
                    db.SaveChanges();
                }
                return true;
            }
            catch(Exception ex)
            {
                return false; 
            }
        }
    }
}